package com.menchao.carsales.service.impl;

import com.github.pagehelper.PageHelper;
import com.menchao.carsales.common.http.ResponseCode;
import com.menchao.carsales.common.http.ResponseData;
import com.menchao.carsales.dao.CustomerMapper;
import com.menchao.carsales.pojo.Customer;
import com.menchao.carsales.service.ICustomerService;
import com.menchao.carsales.vo.CustomerQuery;
import com.menchao.carsales.vo.ListPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerResponse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Porject: carsales
 * Date: 2021/7/28
 * Author: menchao
 * Description:
 */

@Service
public class CustomerService implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public ResponseData addCustomer(Customer customer) {
        customer.setId(createCustomerId());
        if (customer == null) {
            //SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
            //String format = dateFormat.format(new Date());
            customer.setCreateTime(new Date());
        }
        int resultCount = customerMapper.insert(customer);
        if (resultCount != 0) {
            return ResponseData.success();
        }
        return ResponseData.faile("添加客户失败");
    }

    @Override
    public ResponseData getList(CustomerQuery customerQuery) {

        List<Customer> list = PageHelper.startPage(customerQuery.getPage(),customerQuery.getLimit()).doSelectPage(
                ()-> customerMapper.selectSelective(customerQuery)
        );
        if (list != null){
            ListPage listPage = new ListPage();
            long totalSize =  PageHelper.count(()->customerMapper.selectSelective(customerQuery));
            System.out.println("customer getList totalSize: " + totalSize);
            listPage.setTotal(totalSize);
            listPage.setItems(list);
            listPage.setPageNumber(customerQuery.getPage());
            listPage.setPageSize(customerQuery.getLimit());
            return  ResponseData.success(listPage);
        }
        //获取总数
        return ResponseData.faile("获取客户列表失败");

    }

    @Override
    public ResponseData updateCustomer(Customer customer) {

        System.out.println("update customer : " + customer.toString());
        int resultCount =   customerMapper.update(customer);
        if (resultCount>0){
           return ResponseData.success();
        }else{
            return  ResponseData.faile("更新失败");
        }

    }


    @Override
    public ResponseData deleteCustomer(Customer customer) {
        System.out.println("delete customer : " + customer.toString());
        if (customer.getId()<=0){
            return  ResponseData.errorMessage("用户不存在", ResponseCode.PARAMETER_EXCEPTION.getCode());
        }
        int resultCount =   customerMapper.deleteByPrimaryKey(customer.getId());
        if (resultCount>0){
            return ResponseData.success();
        }else{
            return  ResponseData.faile("删除失败");
        }
    }

    /**
     * 客户编号
     * 格式为：yyMMdd 加 五位递增的数字，数字每天重置为1
     * @return
     */
    private Long createCustomerId() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        String format = dateFormat.format(new Date()) + "10000";
        return Long.valueOf(format) + (num++);
    }

    private int num = 1;
}
